//
//  BrandSelectVC.m
//  danpin
//
//  Created by chuangjia on 6/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "BrandSelectVC.h"

#import "BaseModel.h"
#import "MDSearchBarController.h"
@interface BrandSelectVC ()<UITableViewDataSource,UITableViewDelegate,MDSearchBarViewControllerDelegate,UITextFieldDelegate>
{
    
}
@property(nonatomic,strong)UITableView *tbView;
@property(nonatomic,strong) NSMutableArray *setcionArray;
@property(nonatomic,strong)void(^actionBlock)(id,id);
//
@property (nonatomic, strong) MDSearchBar * searchBar;
@property (nonatomic, strong) MDSearchBarController * searchBarController;
@property (nonatomic,strong) NSMutableArray *searchArray;
@property (nonatomic,strong) NSString *searchWord;
@end

@implementation BrandSelectVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    self.isComment =YES;
    self.navTitle=@"选择品牌";
  
    if (!_setcionArray) {
        _setcionArray =[[NSMutableArray alloc] init];
    }
    if (!_searchArray) {
        _searchArray =[[NSMutableArray alloc] init];
    }
    _searchWord=@"";
    [self initUI];
    [self initSearchView];
    [ZShare showAnimationView:self.view center:CGPointZero];
    [self gataCategoryDataWithWord:@"" isSearch:NO];
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    switch (btn.tag) {
        case 0:
        {
            self.actionBlock =nil;
            self.searchBarController.delegate= nil;
            self.searchBarController.active =NO;
            self.searchBarController=nil;
            self.tbView.tableHeaderView=nil;
            self.tbView.tableFooterView =nil;
            [self.navigationController popViewControllerAnimated:YES];
        }
            break;

        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
      DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)dealloc
{
    [_setcionArray removeAllObjects];
    _setcionArray =nil;
    [_searchArray removeAllObjects];
    _searchArray =nil;
    self.actionBlock =nil;
    self.searchBarController.delegate= nil;
    self.searchBarController.active =NO;
    self.searchBarController=nil;
    self.tbView.tableHeaderView=nil;
    self.tbView.tableFooterView =nil;
    self.tbView =nil;
    DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)setSelectedCallBack:(void(^)(id,id))callback
{
    self.actionBlock=callback;
}

-(void)initUI
{
    _tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navHeight, self.view.bounds.size.width, self.view.bounds.size.height-self.navHeight) style:UITableViewStylePlain];
    _tbView.delegate = self;
    _tbView.dataSource = self;
    _tbView.backgroundColor =[UIColor whiteColor];
    _tbView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tbView.showsVerticalScrollIndicator = NO;
    _tbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_tbView];
}
#pragma mark - tableview
#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==_tbView) {
        return self.setcionArray.count;
    }else
    {
        return self.searchArray.count;
    }
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
        cell.selectedBackgroundView.backgroundColor =UIColorFromRGB(0xf4f4f4);
    }
    
    if (tableView==_tbView) {
        cell.textLabel.font = FONT(14);
        if (indexPath.row>=self.setcionArray.count) {
            return cell;
        }
        NSDictionary *dict =[self.setcionArray objectAtIndex:indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@",[dict objectForKey:@"brand_name"]];
        return cell;
    }
    else
    {
        if (indexPath.row >=self.searchArray.count) {
            return cell;
        }
        NSDictionary *dict =[self.searchArray objectAtIndex:indexPath.row];
        if (_searchWord && _searchWord.length>0) {
            if (indexPath.row==0) {
                cell.textLabel.textColor = [UIColor darkGrayColor];
                cell.textLabel.text = [NSString stringWithFormat:@"新增品牌: %@",[dict objectForKey:@"brand_name"]];
            }else{
                cell.textLabel.font = FONT(14);
                cell.textLabel.text = [NSString stringWithFormat:@"%@",[dict objectForKey:@"brand_name"]];
            }
        }else{
             cell.textLabel.font = FONT(14);
             cell.textLabel.text = [NSString stringWithFormat:@"%@",[dict objectForKey:@"brand_name"]];
        }
     

        return cell;
    }
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView == _tbView)
    {
        if (indexPath.row>=self.setcionArray.count) {
            return;
        }
        NSDictionary *dict =[self.setcionArray objectAtIndex:indexPath.row];
        if (self.actionBlock) {
            self.actionBlock(dict,@"brand");
        }
        self.searchBarController.delegate= nil;
        self.searchBarController.active =NO;
        self.searchBarController=nil;
        self.tbView.tableHeaderView=nil;
        self.tbView.tableFooterView =nil;
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        if (indexPath.row>=self.searchArray.count) {
            return;
        }
        NSDictionary *dict =[self.searchArray objectAtIndex:indexPath.row];
        if (self.actionBlock) {
            self.actionBlock(dict,@"brand");
        }
        self.searchBarController.delegate= nil;
        self.searchBarController.active =NO;
        self.searchBarController=nil;
        self.tbView.tableHeaderView=nil;
        self.tbView.tableFooterView =nil;
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

//////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////
-(void)initSearchView
{
    UIView *headeview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 48)];
    headeview.backgroundColor =[UIColor clearColor];
    self.tbView.tableHeaderView=headeview;
    if (self.searchBar) {
        [self.searchBar removeFromSuperview];
        self.searchBar=nil;
    }
    self.searchBar =[[MDSearchBar alloc] initWithFrame:CGRectMake(0,PHOTO_FRAME_WIDTH,DEVICE_WIDTH, 28)];
    [headeview addSubview:self.searchBar];
    
    if (self.searchBarController)
    {
        self.searchBarController.delegate= nil;
        self.searchBarController.active =NO;
        self.searchBarController=nil;
    }
    self.searchBarController = [[MDSearchBarController alloc] init];
    self.searchBarController.delegate= self;
    self.searchBarController.searchBarView.frame = self.searchBar.frame;
    [self.searchBar.superview addSubview:self.searchBarController.searchBarView];
    [self.searchBar removeFromSuperview];
    self.searchBar = self.searchBarController.searchBarView;
    
    self.searchBarController.active = NO;
    self.searchBar.textField.placeholder = @"搜索品牌...";
}
-(void)searchBar:(MDSearchBarController *)searchBarController searchWithText:(NSString *)text{
    [self gataCategoryDataWithWord:text isSearch:YES];
}
-(void)searchBar:(MDSearchBarController *)searchBarController searchWithActive:(BOOL)active
{
//    [self.searchArray removeAllObjects];
    [self.searchBarController reloadData];
}
-(void)searchBar:(MDSearchBarController *)searchBarController searchWithKeyWord:(NSString *)isSearch
{
    self.searchBarController.active =NO;
    [self gataCategoryDataWithWord:isSearch isSearch:YES];
    
}
//
-(void)gataCategoryDataWithWord:(NSString*)word isSearch:(BOOL)isSearch
{
    _searchWord = word;
    if (isSearch) {
        _searchWord=word;
        self.searchBarController.searchView.searchTable.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-self.navHeight-48);
    }
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:word forKey:@"word"];
    [dict setValue:@"brand" forKey:@"action"];
    [dict setValue:self.cate_id forKey:@"cate_id"];
    [dict setValue:@"" forKey:@"brand_id"];
    [dict setValue:@"0" forKey:@"from"];
    __weak typeof (self) wSelf = self;
    [BaseModel suggestion:dict success:^(AFHTTPRequestOperation *operation, id result) {
        if (isSearch) {
            [wSelf parserSearchResultObject:result word:word];
        }else{
            [wSelf parserResultObject:result];
        }
        [ZShare hiddenAnimationView:wSelf.view];
    } failure:^(NSError *error) {
        [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
}
-(void)parserResultObject:(id)resultObject
{
    if (resultObject==nil) {
        return;
    }
    if (resultObject && [resultObject isKindOfClass:[NSArray class]]) {
        [self.setcionArray removeAllObjects];
        [self.setcionArray addObjectsFromArray:resultObject];
    }
    [_tbView reloadData];
    
}
-(void)parserSearchResultObject:(id)resultObject word:(NSString*)word
{
    if (resultObject && [resultObject isKindOfClass:[NSArray class]]) {
        [self.searchArray removeAllObjects];
        [self.searchArray addObjectsFromArray:resultObject];
    }
    //{"brand_id":"10535","brand_name":"Topshop","cate_id":"300","ware_count":"155","like_count":"25997","follow_count":"0"},
    if(word && word.length>0)
    {
        NSMutableDictionary *dict =[NSMutableDictionary dictionary];
        [dict setValue:@"" forKey:@"brand_id"];
        [dict setValue:word forKey:@"brand_name"];
        [dict setValue:@"" forKey:@"cate_id"];
        [dict setValue:@"" forKey:@"ware_count"];
        [dict setValue:@"" forKey:@"like_count"];
        [dict setValue:@"" forKey:@"follow_count"];
        [self.searchArray insertObject:dict atIndex:0];
    }
    if (self.searchArray.count>0) {
        self.searchBarController.active = YES;
    }
    [self.searchBarController reloadData];
}
@end
